package com.xwn.utils.core;

import com.xwn.utils.core.T.StringUtils;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.http.util.ByteArrayBuffer;
import org.apache.log4j.Logger;

import javax.naming.SizeLimitExceededException;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author duandw
 * @Date 2019/12/31 17:50
 **/
public class FTPUtilsImpl {
    public static final String SEPARATOR;
    public static final int RANGE_FILE_AND_DIR = 0;
    public static final int RANGE_FILE = 1;
    public static final int RANGE_DIR = 2;
    public static final int READ_FILE_SIZE_LIMIT = 10485760;
    private static Logger logger;

    public FTPUtilsImpl() {
    }

    public static FTPClientWithInfos connectFtp(String host, int port, String username, String password, String ftpCharset) throws IOException {
        logger.debug("Try to connect to ftp..." + host);
        FTPClientWithInfos ftp = new FTPClientWithInfos();
        ftp.connect(host, port);
        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            ftp.disconnect();
            throw new IOException("can't connect to FTP host");
        } else {
            if (!StringUtils.isNotBlank(username) && !StringUtils.isNotBlank(password)) {
                ftp.login("anonymous", "anonymous");
            } else {
                ftp.login(username, password);
            }

            if (FTPReply.isPositiveCompletion(ftp.sendCommand("OPTS UTF8", "ON"))) {
                ftpCharset = "UTF-8";
            } else if (org.apache.commons.lang3.StringUtils.isEmpty(ftpCharset)) {
                throw new IllegalArgumentException("Ftp charset is empty, system can not process special language. host:" + host);
            }

            ftp.enterLocalPassiveMode();
            ftp.setBufferSize(65536);
            ftp.setSoTimeout(10000);
            ftp.setDefaultTimeout(10000);
            ftp.setDataTimeout(15000);
            ftp.setFileType(2);
            ftp.setFileTransferMode(12);
            ftp.setControlEncoding(ftpCharset);
            logger.debug("Ftp login succeed.");
            return ftp;
        }
    }

    public static FTPClientWithInfos connectAnyoneFtp(String host, int port, String ftpCharset) throws IOException {
        return connectFtp(host, port, "anonymous", "anonymous", ftpCharset);
    }

    public static void disconnect(FTPClientWithInfos ftpClient) {
        if (null != ftpClient && ftpClient.isConnected()) {
            try {
                logger.debug("Disconnect ftp:" + ftpClient.getRemoteAddress());
                ftpClient.logout();
            } catch (IOException var10) {
                ;
            } finally {
                try {
                    ftpClient.disconnect();
                } catch (IOException var9) {
                    ;
                }

            }
        }

    }

    public static List<FTPFile> listFiles(FTPClientWithInfos ftp, String remoteDirectoryPath) throws IOException {
        return ls(ftp, remoteDirectoryPath, 1);
    }

    public static List<FTPFile> listDirectories(FTPClientWithInfos ftp, String remoteDirectoryPath) throws IOException {
        return ls(ftp, remoteDirectoryPath, 2);
    }

    public static List<FTPFile> listAll(FTPClientWithInfos ftp, String remoteDirectoryPath) throws IOException {
        return ls(ftp, remoteDirectoryPath, 0);
    }

    private static List<FTPFile> ls(FTPClientWithInfos ftp, String remotePath, int range) throws IOException {
        List<FTPFile> nameList = new ArrayList();
        int count;
        FTPFile[] fs;
        FTPFile[] var6;
        int var7;
        int var8;
        FTPFile ftpFile;
        if (0 == range || 1 == range) {
            count = 0;
            fs = ftp.listFiles(CharSetAdapter(ftp, remotePath));
            var6 = fs;
            var7 = fs.length;

            for(var8 = 0; var8 < var7; ++var8) {
                ftpFile = var6[var8];
                if (ftpFile.isFile()) {
                    nameList.add(ftpFile);
                    ++count;
                }
            }

            logger.debug("List files:\"" + remotePath + "\",count=" + count);
        }

        if (0 == range || 2 == range) {
            count = 0;
            fs = ftp.listDirectories(CharSetAdapter(ftp, remotePath));
            var6 = fs;
            var7 = fs.length;

            for(var8 = 0; var8 < var7; ++var8) {
                ftpFile = var6[var8];
                String name = ftpFile.getName();
                if (ftpFile.isDirectory() && !name.equals(".") && !name.equals("..")) {
                    nameList.add(ftpFile);
                    ++count;
                }
            }

            logger.debug("List directorys:\"" + remotePath + "\",count=" + count);
        }

        return nameList;
    }

    public static List<String> listFilePathRecusion(FTPClientWithInfos ftp, String remotePath) throws IOException {
        return listFilePathRecusion(ftp, remotePath, new LinkedList());
    }

    private static List<String> listFilePathRecusion(FTPClientWithInfos ftp, String remotePath, List<String> filePathResult) throws IOException {
        FTPFile[] fileList = ftp.listFiles(CharSetAdapter(ftp, remotePath));
        FTPFile[] directoriesList = fileList;
        int var5 = fileList.length;

        int var6;
        for(var6 = 0; var6 < var5; ++var6) {
            FTPFile file = directoriesList[var6];
            if (file.isFile()) {
                filePathResult.add((SEPARATOR.equals(remotePath) ? "" : remotePath) + SEPARATOR + file.getName());
            }
        }

        directoriesList = ftp.listDirectories(CharSetAdapter(ftp, remotePath));
        FTPFile[] var10 = directoriesList;
        var6 = directoriesList.length;

        for(int var11 = 0; var11 < var6; ++var11) {
            FTPFile director = var10[var11];
            String name = director.getName();
            if (director.isDirectory() && !name.equals(".") && !name.equals("..")) {
                listFilePathRecusion(ftp, (SEPARATOR.equals(remotePath) ? "" : remotePath) + SEPARATOR + name, filePathResult);
            }
        }

        return filePathResult;
    }

    public static FTPFile getFileInfo(FTPClientWithInfos ftp, String remoteFilePath) throws IOException {
        FTPFile[] ftpFiles = ftp.listFiles(CharSetAdapter(ftp, remoteFilePath));
        if (ftpFiles.length == 0) {
            throw new FileNotFoundException("file not found in ftp server, remote path=" + remoteFilePath);
        } else {
            return ftpFiles[0];
        }
    }

    public static FTPFile getDirectoryInfo(FTPClientWithInfos ftp, String remoteDirectoryPath) throws IOException {
        String[] arr = remoteDirectoryPath.split(SEPARATOR);
        String dirName = arr[arr.length - 1];
        int index = remoteDirectoryPath.lastIndexOf(dirName, remoteDirectoryPath.length());
        String dirParent = remoteDirectoryPath.substring(0, index);
        FTPFile[] fs = ftp.listDirectories(CharSetAdapter(ftp, dirParent));
        FTPFile[] var7 = fs;
        int var8 = fs.length;

        for(int var9 = 0; var9 < var8; ++var9) {
            FTPFile f = var7[var9];
            if (f.getName().equals(dirName)) {
                return f;
            }
        }

        return null;
    }

    public static boolean makeDirectory(FTPClientWithInfos ftp, String remoteDirectoryPath) throws IOException {
        if (SEPARATOR.equals(remoteDirectoryPath)) {
            return true;
        } else {
            if (!remoteDirectoryPath.endsWith(SEPARATOR)) {
                remoteDirectoryPath = remoteDirectoryPath + SEPARATOR;
            }

            FTPFile dirInfo = getDirectoryInfo(ftp, remoteDirectoryPath);
            if (null == dirInfo) {
                ftp.changeWorkingDirectory(SEPARATOR);
                int start = 0;
                if (remoteDirectoryPath.startsWith(SEPARATOR)) {
                    start = 1;
                }

                int end = remoteDirectoryPath.indexOf(SEPARATOR, start);

                while(true) {
                    int separatorIndex = end == -1 ? remoteDirectoryPath.length() - 1 : end;
                    String subDir = remoteDirectoryPath.substring(start, separatorIndex);
                    boolean tryChangeToSub = ftp.changeWorkingDirectory(CharSetAdapter(ftp, subDir));
                    if (!tryChangeToSub) {
                        boolean makeSub = ftp.makeDirectory(CharSetAdapter(ftp, subDir));
                        boolean changeToSub = ftp.changeWorkingDirectory(CharSetAdapter(ftp, subDir));
                        if (!makeSub || !changeToSub) {
                            logger.error("Make directory file failed:" + remoteDirectoryPath + ",[" + subDir + "]");
                            return false;
                        }
                    }

                    start = end + 1;
                    end = remoteDirectoryPath.indexOf(SEPARATOR, start);
                    if (end <= start) {
                        logger.debug("Make directory:" + remoteDirectoryPath);
                        break;
                    }
                }
            }

            return true;
        }
    }

    public static boolean writeFile(FTPClientWithInfos ftp, String remoteDirectoryPath, String remoteFileName, byte[] content) throws IOException {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(content);
        return writeFile(ftp, remoteDirectoryPath, remoteFileName, (InputStream)inputStream);
    }

    public static boolean writeFile(FTPClientWithInfos ftp, String remoteDirectoryPath, String remoteFileName, InputStream input) throws IOException {
        logger.debug("Start write file:name=" + remoteFileName + ",size=" + input.available() + "bytes");
        if (!makeDirectory(ftp, remoteDirectoryPath)) {
            return false;
        } else {
            boolean bSucc = ftp.storeFile(CharSetAdapter(ftp, remoteDirectoryPath + SEPARATOR + remoteFileName), input);
            logger.debug("Write file " + (bSucc ? "successful:" : "failed:") + remoteDirectoryPath + SEPARATOR + remoteFileName);
            return bSucc;
        }
    }

    public static boolean uploadFile(FTPClientWithInfos ftp, String remoteDirectoryPath, String remoteFileName, String localFilePath) throws IOException {
        FileInputStream inputStream = null;

        boolean var7;
        try {
            File localFile = new File(localFilePath);
            if (!localFile.exists()) {
                throw new FileNotFoundException("Upload file failed:" + localFilePath + ", [File does not exist]");
            }

            logger.debug("Start uploading file:name=" + localFilePath + ",size=" + localFile.length() + "bytes");
            boolean bSucc;
            if (!makeDirectory(ftp, remoteDirectoryPath)) {
                bSucc = false;
                return bSucc;
            }

            inputStream = new FileInputStream(new File(localFilePath));
            bSucc = ftp.storeFile(CharSetAdapter(ftp, remoteDirectoryPath + SEPARATOR + remoteFileName), inputStream);
            logger.debug("Upload file " + (bSucc ? "successful:" : "failed:") + remoteDirectoryPath + SEPARATOR + remoteFileName);
            var7 = bSucc;
        } finally {
            try {
                if (null != inputStream) {
                    inputStream.close();
                }
            } catch (Exception var15) {
                ;
            }

        }

        return var7;
    }

    public static boolean uploadDirectory(FTPClientWithInfos ftp, String remoteDirectoryPath, String localDirPath) throws IOException {
        File dir = new File(localDirPath);
        if (!dir.exists()) {
            logger.error("Upload directory failed:" + localDirPath + ", [Directory does not exist]");
            return false;
        } else {
            File[] fileList = dir.listFiles();
            logger.debug("Start uploading file:name=" + localDirPath + ",file list=" + fileList.length);
            int count = 0;
            int total = 0;
            File[] var7 = fileList;
            int var8 = fileList.length;

            for(int var9 = 0; var9 < var8; ++var9) {
                File f = var7[var9];
                if (!uploadRecursion(ftp, remoteDirectoryPath, f.getName(), f)) {
                    return false;
                }

                ++count;
                ++total;
            }

            logger.debug("Upload directory from \"" + localDirPath + "\" to \"" + remoteDirectoryPath + "\" count=" + count + SEPARATOR + total);
            return count > 0;
        }
    }

    private static boolean uploadRecursion(FTPClientWithInfos ftp, String remoteDirectoryPath, String remoteFileName, File localFile) throws IOException {
        FileInputStream inputStream = null;

        try {
            boolean bSucc;
            if (!localFile.isDirectory()) {
                FTPFile fileInfo = getFileInfo(ftp, remoteDirectoryPath + SEPARATOR + remoteFileName);
                if (null != fileInfo) {
                    deleteFile(ftp, remoteDirectoryPath + SEPARATOR + remoteFileName);
                }

                if (!makeDirectory(ftp, remoteDirectoryPath)) {
                    bSucc = false;
                    return bSucc;
                } else {
                    inputStream = new FileInputStream(localFile);
                    bSucc = ftp.storeFile(CharSetAdapter(ftp, remoteDirectoryPath + SEPARATOR + remoteFileName), inputStream);
                    logger.debug("Upload file " + (bSucc ? "successful:" : "failed:") + remoteDirectoryPath + SEPARATOR + remoteFileName);
                    boolean var24 = bSucc;
                    return var24;
                }
            } else {
                File[] list = localFile.listFiles();
                File[] var6 = list;
                int var7 = list.length;

                for(int var8 = 0; var8 < var7; ++var8) {
                    File subFile = var6[var8];
                    if (!uploadRecursion(ftp, remoteDirectoryPath + SEPARATOR + localFile.getName(), subFile.getName(), subFile)) {
                        boolean var10 = false;
                        return var10;
                    }
                }

                bSucc = true;
                return bSucc;
            }
        } finally {
            try {
                if (null != inputStream) {
                    inputStream.close();
                }
            } catch (Exception var20) {
                ;
            }

        }
    }

    public static boolean downloadFile(FTPClientWithInfos ftp, String remoteFilePath, String localFilePath) throws IOException {
        FileOutputStream is = null;

        boolean var7;
        try {
            FTPFile fileInfo = getFileInfo(ftp, remoteFilePath);
            if (null == fileInfo || !fileInfo.isFile()) {
                logger.error("Download file failed:" + remoteFilePath + ", [File does not exist]");
                boolean var17 = false;
                return var17;
            }

            logger.debug("Start downloading file:name=" + remoteFilePath + ",size=" + fileInfo.getSize() + "bytes");
            File localFile = new File(localFilePath);
            if (localFile.exists()) {
                localFile.delete();
            } else {
                File f = new File(localFile.getParent());
                if (!f.exists()) {
                    f.mkdirs();
                }
            }

            is = new FileOutputStream(localFile);
            boolean bSucc = ftp.retrieveFile(CharSetAdapter(ftp, remoteFilePath), is);
            is.flush();
            logger.debug("Download file " + (bSucc ? "successful:" : "failed:") + localFilePath + ", size=" + localFile.length() + "bytes");
            var7 = bSucc;
        } finally {
            try {
                if (null != is) {
                    is.close();
                }
            } catch (Exception var15) {
                ;
            }

        }

        return var7;
    }

    public static boolean downloadDirectory(FTPClientWithInfos ftp, String remoteDirectoryPath, String localDirectoryPath) throws IOException {
        FTPFile directoryInfo = getDirectoryInfo(ftp, remoteDirectoryPath);
        if (null != directoryInfo && directoryInfo.isDirectory()) {
            List<FTPFile> list = ls(ftp, remoteDirectoryPath, 0);
            logger.debug("Start downloading directory:name=" + remoteDirectoryPath + ",file list=" + list.size());
            int count = 0;
            int total = 0;

            for(Iterator var7 = list.iterator(); var7.hasNext(); ++total) {
                FTPFile f = (FTPFile)var7.next();
                if (!downloadRecursion(ftp, remoteDirectoryPath, f, localDirectoryPath)) {
                    return false;
                }

                ++count;
            }

            logger.debug("Download directory from \"" + remoteDirectoryPath + "\" to \"" + localDirectoryPath + "\" count=" + count + SEPARATOR + total);
            return count > 0;
        } else {
            logger.error("Download directory failed:" + remoteDirectoryPath + ", [Directory does not exist]");
            return false;
        }
    }

    private static boolean downloadRecursion(FTPClientWithInfos ftp, String remoteDirectoryPath, FTPFile remoteFile, String localDirectoryPath) throws IOException {
        FileOutputStream is = null;

        boolean var20;
        try {
            boolean var8;
            if (!remoteFile.isDirectory()) {
                String localFilePath = localDirectoryPath + SEPARATOR + remoteFile.getName();
                File localfile = new File(localFilePath);
                if (localfile.exists()) {
                    localfile.delete();
                } else {
                    File f = new File(localfile.getParent());
                    if (!f.exists()) {
                        f.mkdirs();
                    }
                }

                is = new FileOutputStream(localfile);
                boolean bSucc = ftp.retrieveFile(CharSetAdapter(ftp, remoteDirectoryPath + SEPARATOR + remoteFile.getName()), is);
                is.flush();
                logger.debug("Download file " + (bSucc ? "successful:" : "failed:") + localFilePath + ", size=" + localfile.length() + "bytes");
                var8 = bSucc;
                return var8;
            }

            List<FTPFile> list = ls(ftp, remoteDirectoryPath + SEPARATOR + remoteFile.getName(), 0);
            Iterator var6 = list.iterator();

            while(var6.hasNext()) {
                FTPFile subFile = (FTPFile)var6.next();
                if (!downloadRecursion(ftp, remoteDirectoryPath + SEPARATOR + remoteFile.getName(), subFile, localDirectoryPath + SEPARATOR + remoteFile.getName())) {
                    var8 = false;
                    return var8;
                }
            }

            var20 = true;
        } finally {
            try {
                if (null != is) {
                    is.close();
                }
            } catch (Exception var17) {
                ;
            }

        }

        return var20;
    }

    public static ByteArrayOutputStream readFileStream(FTPClientWithInfos ftp, String remoteFilePath) throws IOException, SizeLimitExceededException {
        InputStream in = null;

        try {
            in = internalReadFileFromStream(ftp, remoteFilePath);
            byte[] b = new byte[1024];
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

            int c;
            while((c = in.read(b)) != -1) {
                outputStream.write(b, 0, c);
            }

            logger.debug("Read file stream done:name=" + remoteFilePath + ",size=" + outputStream.size() + "bytes");
            ByteArrayOutputStream var6 = outputStream;
            return var6;
        } finally {
            completePendingSilence(ftp, in);
        }
    }

    private static void completePendingSilence(FTPClientWithInfos ftp, InputStream in) {
        try {
            if (null != in) {
                in.close();
            }
        } catch (IOException var11) {
            ;
        } finally {
            try {
                if (ftp != null && null != in) {
                    ftp.completePendingCommand();
                }
            } catch (IOException var10) {
                ;
            }

        }

    }

    public static byte[] readFileBytes(FTPClientWithInfos ftp, String remoteFilePath) throws IOException, SizeLimitExceededException {
        InputStream in = null;
        byte[] bs = new byte[32];

        try {
            in = internalReadFileFromStream(ftp, remoteFilePath);
            ByteArrayBuffer bb = new ByteArrayBuffer(102400);

            int len;
            while((len = in.read(bs)) >= 0) {
                bb.append(bs, 0, len);
            }

            byte[] var6 = bb.toByteArray();
            return var6;
        } finally {
            completePendingSilence(ftp, in);
        }
    }

    private static InputStream internalReadFileFromStream(FTPClientWithInfos ftp, String remoteFilePath) throws IOException, SizeLimitExceededException {
        FTPFile fileInfo = getFileInfo(ftp, remoteFilePath);
        if (null != fileInfo && fileInfo.isFile()) {
            if (fileInfo.getSize() > 10485760L) {
                throw new SizeLimitExceededException("Read file stream failed:" + remoteFilePath + ", [File size > 100M]");
            } else {
                logger.debug("Start reading file:name=" + remoteFilePath + ",size=" + fileInfo.getSize());
                InputStream in = ftp.retrieveFileStream(CharSetAdapter(ftp, remoteFilePath));
                if (in == null) {
                    throw new FileNotFoundException("Read file stream failed:" + remoteFilePath + ", [File does not exist]");
                } else {
                    return in;
                }
            }
        } else {
            throw new FileNotFoundException("Read file stream failed:" + remoteFilePath + ", [File does not exist]");
        }
    }

    public static boolean deleteFile(FTPClientWithInfos ftp, String remoteFilePath) throws IOException {
        FTPFile fileInfo = getFileInfo(ftp, remoteFilePath);
        if (null == fileInfo) {
            throw new FileNotFoundException("The file to delete was not found, path = " + remoteFilePath);
        } else {
            boolean bSucc = ftp.deleteFile(CharSetAdapter(ftp, remoteFilePath));
            logger.debug("Delete file " + (bSucc ? "successful:" : "failed:") + remoteFilePath);
            return bSucc;
        }
    }

    public static boolean deleteDirectory(FTPClientWithInfos ftp, String remoteDirectoryPath, boolean bForce) throws IOException {
        FTPFile dirInfo = getDirectoryInfo(ftp, remoteDirectoryPath);
        if (null == dirInfo) {
            return false;
        } else {
            boolean bSucc = false;
            int count = 0;
            int total = 0;
            if (dirInfo.isFile()) {
                bSucc = ftp.deleteFile(CharSetAdapter(ftp, remoteDirectoryPath));
                ++count;
                total = total + 1;
            } else {
                List<FTPFile> list = ls(ftp, remoteDirectoryPath, 0);
                total = list.size();
                if (bForce) {
                    if (list.size() > 0) {
                        logger.debug("Start delete directory:" + remoteDirectoryPath);
                    }

                    Iterator var8 = list.iterator();

                    while(var8.hasNext()) {
                        FTPFile f = (FTPFile)var8.next();
                        if (deleteRecursion(ftp, remoteDirectoryPath, f)) {
                            ++count;
                        }
                    }
                }

                bSucc = ftp.removeDirectory(CharSetAdapter(ftp, remoteDirectoryPath));
            }

            logger.debug("Delete directory " + (bSucc ? "successful:" : "failed:") + remoteDirectoryPath + ",count=" + count + SEPARATOR + total);
            return bSucc;
        }
    }

    private static boolean deleteRecursion(FTPClientWithInfos ftp, String remoteDirectoryPath, FTPFile remoteFile) throws IOException {
        if (!remoteFile.isDirectory()) {
            boolean bSucc = ftp.deleteFile(CharSetAdapter(ftp, remoteDirectoryPath + SEPARATOR + remoteFile.getName()));
            logger.debug("Delete file " + (bSucc ? "successful:" : "failed:") + remoteDirectoryPath + SEPARATOR + remoteFile.getName());
            return bSucc;
        } else {
            List<FTPFile> list = ls(ftp, remoteDirectoryPath + SEPARATOR + remoteFile.getName(), 0);
            Iterator var4 = list.iterator();

            while(var4.hasNext()) {
                FTPFile subFile = (FTPFile)var4.next();
                deleteRecursion(ftp, remoteDirectoryPath + SEPARATOR + remoteFile.getName(), subFile);
            }

            boolean bSucc = ftp.removeDirectory(CharSetAdapter(ftp, remoteDirectoryPath + SEPARATOR + remoteFile.getName()));
            logger.debug("Delete directory " + (bSucc ? "successful:" : "failed:") + remoteDirectoryPath + SEPARATOR + remoteFile.getName());
            return bSucc;
        }
    }

    public static boolean renameFile(FTPClientWithInfos ftp, String remoteSrcFilePath, String remoteDstFilePath) throws IOException {
        String[] arr = remoteDstFilePath.split("/");
        String dirName = arr[arr.length - 1];
        int index = remoteDstFilePath.lastIndexOf(dirName, remoteDstFilePath.length());
        String dirParent = remoteDstFilePath.substring(0, index);
        makeDirectory(ftp, dirParent);
        boolean bSucc = ftp.rename(remoteSrcFilePath, remoteDstFilePath);
        logger.debug("Rename file " + (bSucc ? "successful:" : "failed:") + remoteSrcFilePath + " to " + remoteDstFilePath);
        return bSucc;
    }

    public static List<FTPFile> quickListFiles(String host, int port, String username, String password, String ftpCharset, String remotePath) throws IOException {
        FTPClientWithInfos ftp = connectFtp(host, port, username, password, ftpCharset);
        List<FTPFile> list = listFiles(ftp, remotePath);
        disconnect(ftp);
        return list;
    }

    public static List<FTPFile> quickListDirectories(String host, int port, String username, String password, String ftpCharset, String remotePath) throws IOException {
        FTPClientWithInfos ftp = connectFtp(host, port, username, password, ftpCharset);
        List<FTPFile> list = listDirectories(ftp, remotePath);
        disconnect(ftp);
        return list;
    }

    public static List<FTPFile> quickList(String host, int port, String username, String password, String ftpCharset, String remotePath) throws IOException {
        FTPClientWithInfos ftp = connectFtp(host, port, username, password, ftpCharset);
        List<FTPFile> list = listAll(ftp, remotePath);
        disconnect(ftp);
        return list;
    }

    public static boolean quickWriteFile(String host, int port, String username, String password, String ftpCharset, String remotePath, String remoteFileName, byte[] content) throws IOException {
        FTPClientWithInfos ftp = connectFtp(host, port, username, password, ftpCharset);
        boolean bSucc = writeFile(ftp, remotePath, remoteFileName, content);
        disconnect(ftp);
        return bSucc;
    }

    public static boolean quickUploadFile(String host, int port, String username, String password, String ftpCharset, String remotePath, String remoteFileName, String localFilePath) throws IOException {
        FTPClientWithInfos ftp = connectFtp(host, port, username, password, ftpCharset);
        boolean bSucc = uploadFile(ftp, remotePath, remoteFileName, localFilePath);
        disconnect(ftp);
        return bSucc;
    }

    public static boolean quickUploadDirectory(String host, int port, String username, String password, String ftpCharset, String remoteDirectoryPath, String localDirPath) throws IOException {
        FTPClientWithInfos ftp = connectFtp(host, port, username, password, ftpCharset);
        boolean bSucc = uploadDirectory(ftp, remoteDirectoryPath, localDirPath);
        disconnect(ftp);
        return bSucc;
    }

    public static boolean quickDownloadFile(String host, int port, String username, String password, String ftpCharset, String remoteFilePath, String localFilePath) throws IOException {
        FTPClientWithInfos ftp = connectFtp(host, port, username, password, ftpCharset);
        boolean bSucc = downloadFile(ftp, remoteFilePath, localFilePath);
        disconnect(ftp);
        return bSucc;
    }

    public static boolean quickDownloadDirectory(String host, int port, String username, String password, String ftpCharset, String remoteDirectoryPath, String localDirectoryPath) throws IOException {
        FTPClientWithInfos ftp = connectFtp(host, port, username, password, ftpCharset);
        boolean bSucc = downloadDirectory(ftp, remoteDirectoryPath, localDirectoryPath);
        disconnect(ftp);
        return bSucc;
    }

    public static ByteArrayOutputStream quickReadFileStream(String host, int port, String username, String password, String ftpCharset, String remoteFilePath) throws IOException, SizeLimitExceededException {
        FTPClientWithInfos ftp = connectFtp(host, port, username, password, ftpCharset);
        ByteArrayOutputStream outputStream = readFileStream(ftp, remoteFilePath);
        disconnect(ftp);
        return outputStream;
    }

    public static boolean quickDeleteFile(String host, int port, String username, String password, String ftpCharset, String remoteFilePath) throws IOException {
        FTPClientWithInfos ftp = connectFtp(host, port, username, password, ftpCharset);
        boolean bSucc = deleteFile(ftp, remoteFilePath);
        disconnect(ftp);
        return bSucc;
    }

    public static boolean quickDeleteDirectory(String host, int port, String username, String password, String ftpCharset, String remoteDirectoryPath, boolean bForce) throws IOException {
        FTPClientWithInfos ftp = connectFtp(host, port, username, password, ftpCharset);
        boolean bSucc = deleteDirectory(ftp, remoteDirectoryPath, bForce);
        disconnect(ftp);
        return bSucc;
    }

    public static boolean quickRenameFile(String host, int port, String username, String password, String ftpCharset, String remoteSrcFilePath, String remoteDstFilePath) throws IOException {
        FTPClientWithInfos ftp = connectFtp(host, port, username, password, ftpCharset);
        boolean bSucc = renameFile(ftp, remoteSrcFilePath, remoteDstFilePath);
        disconnect(ftp);
        return bSucc;
    }

    private static String CharSetAdapter(FTPClientWithInfos ftp, String src) {
        try {
            return new String(src.getBytes(ftp.getFtpCharset()), "ISO-8859-1");
        } catch (Exception var3) {
            return src;
        }
    }

    public static void main(String[] args) throws Exception {
        ByteArrayBuffer byteArrayBuffer = new ByteArrayBuffer(1024);
        int b = 1;
        byteArrayBuffer.append(b);
        System.out.println(byteArrayBuffer.toByteArray().length);
    }

    static {
        SEPARATOR = File.separator;
        logger = Logger.getLogger(FTPUtilsImpl.class);
    }
}

